//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================
#include "aura.h"
#include "wince.h"
#include "private.h"

AURA_API AuraSystem_GetTime(
        /* [out] */ Aura_SystemTime * pTime)
{
    SYSTEMTIME systime;

    assert(NULL != pTime);

    memset(&systime, 0, sizeof(systime));
    GetSystemTime(&systime);
    WinSysTimeConvertToAura(&systime, pTime);

    return AURA_NOERROR;
}

AURA_API AuraSystem_SetTime(
        /* [in] */ Aura_SystemTime * pTime)
{
    SYSTEMTIME systime;
    BOOL bSuccess = FALSE;

    assert(NULL != pTime);

    memset(&systime, 0, sizeof(systime));
    AuraSysTimeConvertToWin(pTime, &systime);
    bSuccess = SetSystemTime(&systime);
    if (!bSuccess) {
        return GetLastECodeError();
    }

    return AURA_NOERROR;
}

DWORD g_dwStartTickCount;
AURA_API AuraSystem_GetElapsedTime(
        /* [out] */ Aura_Millisecond32 * pTime)
{
    assert(NULL != pTime);
    *pTime = GetTickCount() - g_dwStartTickCount;
    return AURA_NOERROR;
}

AURA_API AuraSystem_GetIdleTime(
        /* [out] */ Aura_Millisecond32 * pTime)
{
    return AURA_E_NOT_IMPLEMENTED;
}

AURA_API AuraSystem_GetTickCount(
        /* [out] */ Aura_Int64 * pTicks)
{
    assert(NULL != pTicks);
    *pTicks = (Aura_Int64)GetTickCount();
    return AURA_NOERROR;
}

AURA_API AuraSystem_GetLoopCountPerMicrosecond(
        /* [out] */ Aura_UInt32 * pLoopCount)
{
    Aura_Int32 u = 0, testCount = 100;
    LARGE_INTEGER litmp;
    Aura_Int64 iStart, iEnd;
    double dfMinus, dfFreq, dfTime;

    assert(NULL != pLoopCount);

    QueryPerformanceFrequency(&litmp);
    dfFreq = (double)litmp.QuadPart;

    QueryPerformanceCounter(&litmp);
    iStart = litmp.QuadPart;
    while (u < testCount) {
        u++;
    }
    QueryPerformanceCounter(&litmp);
    iEnd = litmp.QuadPart;

    dfMinus = (double)(iEnd-iStart);
    dfTime = (dfMinus / dfFreq) * 1000000; //change to microseconds

    *pLoopCount = (Aura_UInt32)(testCount / dfTime);

    if (*pLoopCount == 0) *pLoopCount = 1;

    return AURA_NOERROR;
}

AURA_API AuraSystem_GetMemorySizes(
        /* [out] */ Aura_Int32 * pTotalPhysicalMemorySize,
        /* [out] */ Aura_Int32 * pAvailablePhysicalMemorySize)
{
    MEMORYSTATUS memoryStatus;

    if ((NULL == pTotalPhysicalMemorySize)
        && (NULL == pAvailablePhysicalMemorySize)) {
        return AURA_E_INVALID_ARGUMENT;
    }

    GlobalMemoryStatus(&memoryStatus);
    if (NULL != pTotalPhysicalMemorySize) {
        *pTotalPhysicalMemorySize = memoryStatus.dwTotalPhys;
    }
    if (NULL != pAvailablePhysicalMemorySize) {
        *pAvailablePhysicalMemorySize = memoryStatus.dwAvailPhys;
    }

    return AURA_NOERROR;
}

AURA_API AuraSystem_ErrorPrint(
        /* [in] */ Aura_PWChar wstr)
{
    return AURA_NOERROR;
}

AURA_API AuraSystem_TryWaitForAnyEvent(
        /* [in] */ Aura_Int32 nCount,
        /* [in] */ const Aura_Handle * pEvents,
        /* [in] */ Aura_Int32 msTimeout,
        /* [out] */ Aura_WaitResult * pResult,
        /* [out] */ Aura_Int32 * pIndex,
        /* [out] */ Aura_EventState * pState)
{
    return AURA_NOERROR;
}
